package com.rrd.dw.mr.gzip.kafka;

import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.third.tongdun.TongDunRiskHitRuleBack;
import com.ucredit.babel.common.model.third.tongdun.TongDunRiskRecord;
import com.ucredit.babel.common.model.third.tongdun.TongDunRiskResult;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;
import java.util.List;

/**
 * 同盾-风险决策命中规则
 *
 * @author guoqiang
 */

public class TongDunRiskHitRulesV2Parse extends ParseBase {
    private Text val = new Text();
    private Text key = new Text();

    /**
     * 同盾-风险决策命中规则
     *
     * @param model
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        TongDunRiskRecord obj = (TongDunRiskRecord) model;
        TongDunRiskResult risk_obj = obj.getFraud_result() == null ? new TongDunRiskResult() : obj.getFraud_result();
        StringBuilder sb = getBaseInfo(obj, successful);
        StringBuilder txSb = getBaseInfo(obj);

        StringBuilder TxSb = new StringBuilder(500);
        List<TongDunRiskHitRuleBack> rules =  risk_obj.getHit_rules();
        if(rules == null) {
            return true;
        }
        for(TongDunRiskHitRuleBack rule: rules){
            TxSb.delete(0, TxSb.length());
            TxSb.append(formatValue(rule.getDecision())).append(CRT001);
            TxSb.append(formatValue(rule.getScore())).append(CRT001);
            TxSb.append(formatValue(rule.getName())).append(CRT001);
            TxSb.append(formatValue(rule.getUuid())).append(CRT001);
            TxSb.append(formatValue(rule.getParentUuid())).append(CRT001);
            TxSb.append(formatValue(rule.getId())).append(CRT001);
            key.set(getKey());
            val.set((sb.toString() + TxSb.toString() + txSb.toString()));
            context.write(key, val);
        }
        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_tongdun_risk_hitrules_v2_inc";
    }

    @Override
    public String getSubType() {
        return ".hitrules";
    }

    @Override
    public String getType() {
        return RecordType.TONGDUN_RISK_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "同盾-风险决策命中规则";
    }
}
